app.controller('ScoreList', function($scope, $http, $location, common){
	$scope.timeType 	= 0;
	$scope.object 	= {};
	$scope.listT0 	= {list: new Array()}
	$scope.listT1 	= {list: new Array()}
	$scope.listT2	= {list: new Array()}
	$scope.nPage 	= 1;
	$scope.pageHtml = '';
	$scope.keyword 	= '';
	$scope.type 	= $location.path() == '/Score/getInList' ? 1 : 2;

	//初始化js
	$scope.init_js 	= function(){
		//标签页钩子设置
        $('a[data-toggle="tab"]').on('show.bs.tab', function(e){
            var type    = $(this).data('target');
            switch(type){
                case '#list-all'	: $scope.timeType 	= 0; break;
                case '#list-time-7' : $scope.timeType   = 1; break;
                case '#list-time-30': $scope.timeType   = 2; break;
                default: return false; break;
            }
            $scope.load_data(1);
        });

        //回车键监听
	    common.listenEnterKey(function(){
	        $scope.load_data(1);
	    });
	}

	//加载数据
	$scope.load_data  	= function(page){
		switch($scope.timeType){
			case 0: $scope.object 	= $scope.listT0; break;
			case 1: $scope.object 	= $scope.listT1; break;
			case 2: $scope.object 	= $scope.listT2; break;
			default: return false; break;
		}

		var url 	= config.prefix_url+'/Score/getList?'+
			'&time_type='+$scope.timeType+
			'&keyword='+$scope.keyword+
			'&p='+page+
			'&type='+$scope.type;
		console.log(url);
		$http.get(url).success(function(info){
			$scope.load_style('stop');
			if(info.code != 1){
				common.dealError(info);
				return false;
			}
			$scope.object.list 	= info.data.list;
			$scope.pageHtml 	= info.data.page_html;
			$scope.nPage 	= page;
			common.startPage($scope.load_data);
		});
		$scope.load_style('start');
	}

	//‘加载’样式
    $scope.load_style   = function(type){
        switch(type){
            case 'start':
                $('.title').children('.fa-spinner').css('display', 'inline');
            break;
            case 'stop':
                $('.title').children('.fa-spinner').css('display', 'none');
            break;
        }
    }

    //导出excel
    $scope.out_excel 	= function(){
    	var url 	= config.prefix_url+'/Score/outExcel?'+
    		'&keyword='+$scope.keyword+
    		'&time_type='+$scope.timeType+
    		'&type='+$scope.type;

    	$http.get(url).success(function(info){
    		if(info.code != 1){
    			common.dealError(info);
    			return false;
    		}
    		var count 	= parseInt(info.data.count);
    		var length 	= 5000;
    		var start 	= 0;
    		while(start < count){
    			var _url 	= url + '&start='+start+'&length='+length;
		    	window.open(_url, '_blank');
    			start 	+= length;
    		}
    	});
    }

	$scope.load_data(1);
	$scope.init_js();
})